# coding: utf-8
import json
import random
import sys, os
BASE_PATH = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(BASE_PATH)# 项目目录

def merge(data):
    res = {"ip":[]}
    temp_list = []
    if len(data) == 1:
        return data[0]
    for item in data:
        temp_list.extend(item["ip"])
    res["ip"] = temp_list
    return res

def save_xila(data,model):
    data = merge(data)
    if model == "w":
        with open(BASE_PATH+'\\ip_local_data\\xila_ip.json', 'w', encoding="utf-8") as f:
            json.dump(data,f, indent=4)

def get_by_xila(num=1):
    with open(BASE_PATH+'\\ip_local_data\\xila_ip.json', 'r', encoding="utf-8") as f:
        data = json.load(f)
    data_list = data["ip"]
    temp_proxy = {}
    res = []
    for item in range(0,num):
        temp = random.choice(data_list)
        temp_proxy[temp["http"]] = temp["http"]+"://"+temp["ip"]+":"+temp["port"]
        res.append(temp_proxy.copy())
        pass

    return res

if __name__ == "__main__":
    res = get_by_xila(4)
    print(res)
